package top.forgirl.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.forgirl.domain.Apply;
import top.forgirl.domain.dto.ApplyDo;
import top.forgirl.mapper.ApplyMapper;

import java.util.List;

@Service
public class ApplyService {

    @Autowired
    private ApplyMapper applyMapper;

    public List<Apply> list(ApplyDo applyDo, int offset, int count) {
        applyDo.setName(applyDo.getName().replaceAll(" ", "%"));
        applyDo.setDName(applyDo.getDName().replaceAll(" ", "%"));
        return applyMapper.list(applyDo, offset, count);
    }

    public Apply insert(ApplyDo applyDo) {
        Apply apply = new Apply();
        apply.setCId(applyDo.getCId());
        apply.setDId(applyDo.getDId());
        apply.setName(applyDo.getName());
        apply.setPhoneNumber(applyDo.getPhoneNumber());
        apply.setPrincipal(applyDo.getPrincipal());

        int result = applyMapper.insert(apply);
        if (result > 0) {
            return apply;
        } else {
            return null;
        }
    }

    public Apply delete(ApplyDo applyDo) {
        Apply apply = applyMapper.selectOne(applyDo.getId());
        if (apply != null) {
            int result = applyMapper.delete(apply);
            if (result > 0) {
                return apply;
            }
        }

        return apply;

    }
}
